package com.example.demo.controller;

import com.aliyun.oss.OSS;
import com.example.demo.configuration.AliyunOssConfig;
import com.example.demo.controller.bean.Result;
import com.example.demo.dto.AttributeListRequestDTO;
import com.example.demo.dto.ClothingRequestDTO;
import com.example.demo.mapper.entity.*;
import com.example.demo.service.BadgeManageService;
import com.example.demo.service.ClothingManageService;
import com.example.demo.utils.PageBo;
import com.example.demo.utils.PageVo;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RequestMapping("clothingManage")
@Api(value = "虚拟服饰接口", tags = {"虚拟服饰接口"})
@RestController
@Slf4j
public class ClothingManageController {
    @Autowired
    private ClothingManageService service;

    private OSS ossClient;

    @ResponseBody
    @ApiOperation(value = "获取虚拟服饰列表接口")
    @RequestMapping(value = "getClothingList", method = {RequestMethod.POST})
    public Result<PageVo<Clothing>> getClothingList(@RequestBody PageBo pageBo) {
        PageVo<Clothing> list = service.getClothingList(pageBo);
        return Result.ok(list);
    }

    @ResponseBody
    @ApiOperation(value = "搜索虚拟服饰列表接口")
    @RequestMapping(value = "searchClothingList", method = {RequestMethod.POST})
    public Result<PageVo<ClothingRequestDTO>> getListSearch(@RequestBody ClothingRequestDTO request) {
        PageBo pageBo = new PageBo();
        pageBo.setPageNum(request.getPageNum());
        pageBo.setPageSize(request.getPageSize());

        List<Clothing> list = service.searchClothingList(request);
        PageVo<ClothingRequestDTO> logs = new PageVo<>(pageBo,list);

        return Result.ok(logs);
    }



    @ResponseBody
    @ApiOperation("修改虚拟服饰接口")
    @RequestMapping(value = "updateClothingList", method = {RequestMethod.POST})
    public Result<String> updateBadgeList(@RequestBody Clothing request) {
        return service.updateClothingList(request);
    }


    @ApiOperation(value = "虚拟服饰圖片上传")
    @PostMapping("/addClothing")
    public Result<String> uploadCommodity(@RequestParam("file") MultipartFile file, @RequestParam("addData") String addData) throws IOException {
        log.info(file.toString());

        // 处理传入的 addData
        Clothing request = new ObjectMapper().readValue(addData, Clothing.class);
        System.out.println(request.getClothingName());
        System.out.println(request.getType());
        System.out.println(request.getGender());
        log.info(file.toString());

        String originalFileName = file.getOriginalFilename();//从上传的文件中获取原始文件名
        int i = originalFileName.lastIndexOf(".");
        String png = originalFileName.substring(i);//获取文件扩展名

        //存储空间的名字
        String bucketName = "seedling-classmate";

        //创建存储对象
        ossClient = AliyunOssConfig.ossClient();

        //设置文件名并上传到OSS
        String fileName = "clothing/" + UUID.randomUUID().toString().substring(0,24)+png;
        ossClient.putObject(bucketName, fileName, file.getInputStream());   //之后在数据库中保存fileName

        request.setImageUrl(fileName);
        ossClient.shutdown();
        return service.addClothing(request);

    }


    @ResponseBody
    @ApiOperation("虚拟服饰圖片显示接口")
    @RequestMapping(value = "findClothingPic/{clothingId}")
    public Result<String> findClothingPic(@PathVariable("clothingId")int clothingId) {
        Result<String> message = new Result<>();
        if (clothingId != 0) {
            Clothing result = service.getById(clothingId);
            String imageUrl = result.getImageUrl();
            if(imageUrl != null){
                message.setData("https://seedling-classmate.oss-cn-beijing.aliyuncs.com/" + imageUrl);
                return message;
            }
            else{
                return null;
            }
        }
        return null;
    }

    @ApiOperation(value = "虚拟服饰圖片更新")
    @PostMapping("/updateClothingPic")
    public Result updateClothingPic(@RequestParam("clothingId") int clothingId, @RequestParam("file") MultipartFile file) throws IOException {

        log.info(file.toString());

        String originalFileName = file.getOriginalFilename();//从上传的文件中获取原始文件名
        int i = originalFileName.lastIndexOf(".");
        String png = originalFileName.substring(i);//获取文件扩展名

        //存储空间的名字
        String bucketName = "seedling-classmate";

        //创建存储对象
        ossClient = AliyunOssConfig.ossClient();

        //设置文件名并上传到OSS
        String fileName = "clothing/" + UUID.randomUUID().toString().substring(0,24)+png;
        ossClient.putObject(bucketName, fileName, file.getInputStream());   //之后在数据库中保存fileName

        Clothing currentClothing = service.getById(clothingId);
        currentClothing.setImageUrl(fileName);
        service.placeBadgePic(currentClothing);
        ossClient.shutdown();

        String result="http://seedling-classmate.oss-cn-beijing.aliyuncs.com/"+fileName;
        return Result.ok("上传成功",result);

    }

    @ResponseBody
    @ApiOperation("删除虚拟服饰接口")
    @RequestMapping(value = "deleteClothing", method = {RequestMethod.POST})
    public Result<String> delete(@RequestBody Clothing clothing) {
        return service.deleteClothing(clothing);
    }

    @ResponseBody
    @ApiOperation("批量删除接口")
    @RequestMapping(value ="batchDeleteClothing", method = {RequestMethod.POST})
    public Result<String> deleteList(@RequestBody List<Clothing> clothingList) {
        service.batchDeleteClothing(clothingList);
        return Result.ok("删除成功");
    }

}
